package com.itheima.service.mongo.impl;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.RandomUtil;
import com.itheima.domain.mongo.RecommendVideo;
import com.itheima.domain.mongo.Video;
import com.itheima.domain.mongo.Voice;
import com.itheima.service.mongo.VoiceService;
import com.itheima.util.ConstantUtil;
import org.apache.dubbo.config.annotation.DubboService;
import org.bson.types.ObjectId;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;

import java.util.List;
import java.util.stream.Collectors;

@DubboService
public class VoiceServiceImpl implements VoiceService {


    @Autowired
    MongoTemplate mongoTemplate;

    @Autowired
    RedisIdService redisIdService;

    @Override
    public void save(Voice voice) {
        mongoTemplate.save(voice);
    }

    @Override
    public Voice findById(ObjectId id) {
        return null;
    }

    /*@Override
    public Voice find(Long userId) {
        List<Voice> voiceList = mongoTemplate.findAll(Voice.class);

        if (CollUtil.isNotEmpty(voiceList)) {
            List<Voice> voices= voiceList.stream().filter(s ->s.getUserId().longValue()!=
                    (userId.longValue())).collect(Collectors.toList());


            List
            if (CollUtil.isNotEmpty(voices)) {
            voices.forEach(voice -> {


            });
            }
        }

        return null;
    }*/

    @Override
    public Voice findOne(Long userId) {
        List<Voice> voiceList = mongoTemplate.findAll(Voice.class);

        if (CollUtil.isNotEmpty(voiceList)) {
            List<Voice> voices = voiceList.stream().filter(s -> s.getUserId().longValue() !=
                    (userId.longValue())).collect(Collectors.toList());

            int i = RandomUtil.randomInt(0, voices.size());

            Voice voice = voices.get(i);

            mongoTemplate.remove(voice);
            return voice;
        }
        return null;
    }
}
